package com.qms.tubu.dialog;

import android.animation.ObjectAnimator;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.DatePicker;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.qms.tubu.R;

import java.util.Calendar;

import tool.lib.EncryptTool;
import tool.lib.entity.BirthBean;

/**
 * Created by Mad on 2015/6/2.
 */
public class TimeDialog extends Dialog{
    private TimeSelectedListener mListener;
    private DatePicker datePicker;
    private Calendar calendar;
    private long mMaxDate,mMinDate;
    private boolean hasMaxDate,hasMinDate;
    private LinearLayout mTimePanel;
    public TimeDialog(Context context) {
        this(context, Calendar.getInstance());
    }
    public  TimeDialog(Context context,Calendar calendar)
    {
        super(context,R.style.TimeDialogStyle);
        this.calendar=calendar;
        hasMaxDate=false;
        hasMinDate=false;
    }
    public TimeDialog (Context context,String date)
    {
        super(context, R.style.TimeDialogStyle);
        calendar=Calendar.getInstance();
        if(date.length()!=0) {
            BirthBean bean = EncryptTool.parseBirthday(date);
            calendar.set(bean.getYear(), bean.getMonth()-1, bean.getDay());
        }
        hasMaxDate=false;
        hasMinDate=false;
    }


    public void setMaxDate(long maxDate) {
        hasMaxDate=true;
        this.mMaxDate = maxDate;
    }

    public void setMinDate(long minDate) {
        hasMinDate=true;
        this.mMinDate = minDate;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_time);
        TextView cancelBtn= (TextView) findViewById(R.id.cancel_btn);
        mTimePanel= (LinearLayout) findViewById(R.id.time_panel);
        cancelBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        this.findViewById(R.id.cancel_panel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        TextView commitBtn= (TextView) findViewById(R.id.commit_btn);

        datePicker= (DatePicker) findViewById(R.id.date_picker);
        datePicker.init(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(calendar.DAY_OF_MONTH), null);
        if(hasMaxDate)
            datePicker.setMaxDate(mMaxDate);
        if(hasMinDate)
            datePicker.setMinDate(mMinDate);
        commitBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                int year = datePicker.getYear();
                int month = datePicker.getMonth() + 1;
                int day = datePicker.getDayOfMonth();
                String dateTime =String.format("%d-%02d-%02d",year,month,day);
                if(mListener!=null)
                {

                    if(mListener.onSelected(new BirthBean(year,month,day),dateTime))
                        dismiss();
                }
            }
        });
    }

    public void setOnTimeSelectedListener(TimeSelectedListener listener){
        this.mListener=listener;
    }
    public interface TimeSelectedListener{
        public boolean onSelected(BirthBean birthday, String birthdayStr);
    }
}
